<%@page import="java.util.Date"%>
<%@page import="java.text.DecimalFormat"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="style.css" rel="stylesheet" type="text/css" />
<%String auctionId = request.getParameter("auctionId"); %>
<title>Auction <%=auctionId%> Info</title>

<script language="JavaScript" type="text/javascript">
function placeBid(highestBid,increment)
{
	var sum = parseInt(highestBid) + parseInt(increment);
	if (document.bidForm.bidAmount.value < sum)
		alert('Error: You must bid at least $' + sum.toFixed(2));
	else
	{
		document.bidForm.submit();
	}

}
</script>

</head>
<body>
<div class="main">
  <div class="header">
    <div class="resize">
      <div class="logo"><img src="images/logo.png" alt="picture" width="171" height="42" border="0" /></a></div>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="slider_resize">
    <div class="clr"></div>
  </div>
  <div class="FBG">
    <div class="clr"></div>
  </div>
  <div class="body" align="center">
<% 
		String mysJDBCDriver = (String)session.getValue("mysJDBCDriver"); 
		String mysURL = (String)session.getValue("mysURL"); 
     	String mysUserID = (String)session.getValue("mysUserID");
    	String mysPassword = (String)session.getValue("mysPassword");
        
        String username = ""+session.getValue("login");
  			java.sql.Connection conn=null;
			try 
			{
            	Class.forName(mysJDBCDriver).newInstance();
    			java.util.Properties sysprops=System.getProperties();
    			sysprops.put("user",mysUserID);
    			sysprops.put("password",mysPassword);
        
				//connect to the database
            			conn=java.sql.DriverManager.getConnection(mysURL,sysprops);
            			System.out.println("Connected successfully to database using JConnect");
            
            			java.sql.Statement stmt1=conn.createStatement();
            			java.sql.Statement stmt2=conn.createStatement();
            			
					java.sql.ResultSet auctionResult = stmt1.executeQuery("select Selling.UserName, Auction.OpenDate, "
							+" Auction.CloseDate, Auction.StartingBid, Auction.Increment, Item.Name, "
							+"Item.YearManufactured, Item.Type from Auction, Selling, Item where Auction.AuctionId ='"
							+ auctionId +"' and Selling.AuctionId ="+ auctionId + " and Item.ItemId = Selling.ItemId");
      	  			java.sql.ResultSet bidResult = stmt2.executeQuery("select C.UserName, B.Amount, B.BidTime from Bid B,"
      	  					+ "Customer C where B.AuctionId = '"+ auctionId + "' and C.UserName = B.UserName order by "
      	  					+ "B.BidTime");
%>
      	  <h1>Auction <%=auctionId%> Info</h1>
<%
//We will figure out the highest bid so far as we go through the results.
float highestBid = 0;

//For formatting USD values with two decimal places
DecimalFormat formatter = new DecimalFormat();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);

if(auctionResult.next())
{
	//Initialize the highest bid to the starting price
	highestBid = auctionResult.getInt(4);
	%>			
    <table border="1">
    	<tr>
    		<td>Seller Name: </td>
    		<td><%=auctionResult.getString(1) %>
    	</tr>
    	<tr>
    		<td>Item Name: </td>
    		<td><%=auctionResult.getString(6) %> (<%=auctionResult.getString(7) %>)</td>
   		</tr>
   		<tr>
   			<td>Category: </td>
   			<td><%=auctionResult.getString(8) %></td>
   		</tr>
   		<tr>
   			<td>Open Date: </td>
   			<td><%=auctionResult.getString(2) %></td>
   		</tr>
   		<tr>
   			<td>Close Date: </td>
   			<td><%=auctionResult.getString(3) %></td>
   		</tr>
    </table>
    <br/>
    <h2>Bid History</h2>
    	<table border="1">
		<tr>
			<th>UserName</th> 
			<th>Amount </th>
			<th>Bid Time</th>
		</tr>
<%
	while(bidResult.next())
	{
		float bidAmount = bidResult.getInt(2);
		if (highestBid < bidAmount)
		{
			highestBid = bidAmount;
		}
%>	
		<tr>
			<td><%=bidResult.getString(1) %></td>
			<td><%="$" + formatter.format(bidAmount) %></td>
			<td><%=bidResult.getString(3) %></td>

<%
	}
%>

	</table>
	<%
		if (highestBid == auctionResult.getFloat(4))
		{
			%><p>The starting Bid is $<%=formatter.format(highestBid) %></p><%
		}
		else
		{
	%>
	<p>The highest Bid is $<%=formatter.format(highestBid) %></p>
		<% 
		}
  if (auctionResult.getDate(3).compareTo(new Date()) <= 0)
   {
	   %>
	   <p>Auction closed</p>
	   <%
   }    	
  else
   {
	   %>
	   <form method="post" name="bidForm" action="PlaceBid.jsp">
	   		<input type="hidden" name="auctionId" value="<%=auctionId%>">
	   		<input type="text" name="bidAmount"/>
	   		<button type="button" name="submitBidButton" onclick="placeBid('<%=highestBid%>','<%=auctionResult.getInt(4)%>')">Place Bid</button>
	   </form>
	   <br/>
	   
	   <%
   }
  
  String status = (String)session.getValue("status");
  if (status.equals("manager"))
  {
	   %><input type="button" value="Home" onclick="window.open('ManagerHomePage.jsp','_self');" /><%
  }
  else if (status.equals("employee"))
  {
	  %><input type="button" value="Home" onclick="window.open('EmployeeHomePage.jsp','_self');" /><%
  }
  else
  {
	  %><input type="button" value="Home" onclick="window.open('CustomerHomePage.jsp','_self');" /><%
  }

  %><input type="button" value="Logout" onclick="window.open('index.htm','_self');" /><%
}
 			} catch(Exception e)
		{
			e.printStackTrace();
			out.print(e.toString());
		}
		finally{
		
			try{conn.close();}catch(Exception ee){};
		}
%>
  </div>
	  <div class="footer">
    <div class="resize" style="background:#141414;">
				<p class="leftt">
					Created by: Andrew Gratta, Sarah Jordan, and Kevin O'Flaherty<br /></p>
      <p class="rightt"><span class="leftt">Copyright. All Rights Reserved</span></p>
      <p class="rightt">Developed for CSE305 at Stony Brook University</p>
      <div class="clr"></div>
    </div>
  </div>
</div>
</body>
</html>